package com.anxin.registerManagement.service;

import cn.hutool.core.util.ObjectUtil;
import com.anxin.registerManagement.bean.dto.SceneDistDto;
import com.anxin.registerManagement.bean.jo.SceneSearchJo;
import com.anxin.registerManagement.bean.persist.Scene;
import com.anxin.registerManagement.bean.persist.SceneDist;
import com.anxin.registerManagement.exception.BusinessException;
import com.anxin.registerManagement.mapper.SceneDistMapper;
import com.anxin.registerManagement.mapper.SceneMapper;
import com.anxin.registerManagement.util.Const;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author anxin
 * @date 2022/3/30 19:33
 */
@Service
public class SceneDistService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SceneDistService.class);

    @Autowired
    private SceneDistMapper sceneDistMapper;

    @Autowired
    private SceneMapper sceneMapper;

    public void createDist(SceneDist sceneDist) {
        checkSceneDist(sceneDist);
        sceneDistMapper.insert(sceneDist);
        LOGGER.info("场景距离添加成功");
    }

    public void updateDist(SceneDist sceneDist) {
        checkSceneDist(sceneDist);
        sceneDistMapper.updateById(sceneDist);
        LOGGER.info("场景距离修改成功");
    }

    public void deleteDist(List<Integer> id) {
        sceneDistMapper.deleteBatchIds(id);
    }

    public List<SceneDistDto> getDistById(Integer id) {
        LambdaQueryWrapper<SceneDist> sceneDistLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sceneDistLambdaQueryWrapper.eq(SceneDist::getStartScene, id);
        List<SceneDist> sceneDists = sceneDistMapper.selectList(sceneDistLambdaQueryWrapper);
        List<SceneDistDto> sceneDistDtos = new ArrayList<>();
        for (SceneDist sceneDist : sceneDists) {
            SceneDistDto sceneDistDto = new SceneDistDto();
            BeanUtils.copyProperties(sceneDist, sceneDistDto);
            Scene scene = sceneMapper.selectById(sceneDist.getEndScene());
            sceneDistDto.setEndScenePersist(scene);
            sceneDistDtos.add(sceneDistDto);
        }
        return sceneDistDtos;
    }

    /**
     * 公共数据校验
     */
    public void checkSceneDist(SceneDist sceneDist) {
        // 起点场景是否存在
        Scene queryStartScene = sceneMapper.selectById(sceneDist.getStartScene());
        if (ObjectUtils.isEmpty(queryStartScene)) {
            throw new BusinessException(Const.START_SCENE_NOT_EIXST);
        }
        // 目的地是否存在
        Scene queryEndScene = sceneMapper.selectById(sceneDist.getEndScene());
        if (ObjectUtils.isEmpty(queryEndScene)) {
            throw new BusinessException(Const.END_SCENE_NOT_EXIST);
        }
        // 这里需要起点和终点唯一
        LambdaQueryWrapper<SceneDist> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SceneDist::getStartScene, sceneDist.getStartScene())
                .eq(SceneDist::getEndScene, sceneDist.getEndScene());
        SceneDist querySceneDist = sceneDistMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(sceneDist.getId())) {
            // id不存在，则是新增操作
            if (!ObjectUtils.isEmpty(querySceneDist)) {
                throw new BusinessException(Const.SCENE_DIST_EXIST);
            }
        } else {
            // id存在，则是修改操作
            if (!ObjectUtils.isEmpty(querySceneDist) && !sceneDist.getId().equals(querySceneDist.getId())) {
                throw new BusinessException(Const.SCENE_DIST_EXIST);
            }
        }
    }

}
